package org.myorg.kafka2mysql.demo;


import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
import org.myorg.kafka2mysql.demo.entitys.Student;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 新建 Source 类 MysqlDatasource.java，该类继承 RichSourceFunction ，实现里面的 open、close、run、cancel 方法；
 */
public class MysqlDatasource extends RichSourceFunction<Student> {

	private PreparedStatement ps;
	private Connection connection;

	/**
	 * open() 方法中建立连接，这样不用每次 invoke 的时候都要建立连接和释放连接。
	 * @param parameters
	 * @throws Exception
	 */
	@Override
	public void open(Configuration parameters) throws Exception {
		super.open(parameters);
		connection = getConnection();
		String sql = "select * from student;";
		ps = this.connection.prepareStatement(sql);
	}

	private Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://10.10.87.38:3306/crm?useUnicode=true&characterEncoding=UTF-8", "crm", "crm@2015");
		} catch (Exception e) {
			System.out.println("-----------mysql get connection has exception , msg = " + e.getMessage());
		}
		return conn;
	}

	/**
	 * 程序执行完毕就可以进行，关闭连接和释放资源的动作了
	 * @throws Exception
	 */
	@Override
	public void close() throws Exception {
		super.close();
		if (null != connection) {
			connection.close();
		}
		if (null != ps) {
			ps.close();
		}

	}

	/**
	 * DataStream 调用一次 run() 方法用来获取数据
	 * @param sourceContext
	 * @throws Exception
	 */
	@Override
	public void run(SourceContext<Student> sourceContext) throws Exception {
		ResultSet resultSet = ps.executeQuery();
		while (resultSet.next()) {
			Student student = new Student(
					resultSet.getInt("id"),
					resultSet.getString("name").trim(),
					resultSet.getString("password").trim(),
					resultSet.getInt("age"));
			sourceContext.collect(student);
		}
	}

	@Override
	public void cancel() {

	}


}
